package cn.clq.ymcc.web.controller;

import cn.clq.ymcc.dto.CourseDto;
import cn.clq.ymcc.service.ICourseService;
import cn.clq.ymcc.domain.Course;
import cn.clq.ymcc.query.CourseQuery;
import cn.clq.ymcc.result.JSONResult;
import cn.clq.ymcc.result.PageList;
import cn.clq.ymcc.vo.CourseDetailInfoVo;
import cn.clq.ymcc.vo.CourseOrderVo;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;

@RestController
@RequestMapping("/course")
public class CourseController {

    @Autowired
    public ICourseService courseService;

    /**
     * @description: 查询课程结算界面的所需数据
     * @param: courseIds
     * @return: cn.clq.ymcc.result.JSONResult
     * @author clqlongbao
     * @date: 2022/7/29 11:02
     */
    @GetMapping("/info/{courseIds}")
    public JSONResult searchCourseOrderInfo(@PathVariable("courseIds") String courseIds){
        CourseOrderVo vo = courseService.searchCourseOrderInfo(courseIds);
        return JSONResult.success(vo);
    }


    /**
     * @description:  课程详情页面查询课程详情
     * @param: courseId
     * @return: cn.clq.ymcc.result.JSONResult
     * @author clqlongbao
     * @date: 2022/7/27 17:59
     */
    @GetMapping("/detail/data/{courseId}")
    public JSONResult searchCourseDetail(@PathVariable("courseId") Long courseId){
        CourseDetailInfoVo vo = courseService.searchCourseDetail(courseId);
        return JSONResult.success(vo);
    }


    /**
     * @description: 发布课程
     * @param: courseId
     * @return: cn.clq.ymcc.result.JSONResult
     * @author clqlongbao
     * @date: 2022/7/24 15:03
     */
    @PostMapping("/onLineCourse/{courseId}")
    public JSONResult onLineCourse(@PathVariable("courseId") Long courseId){
        courseService.onLineCourse(courseId);
        return JSONResult.success();
    }

    /**
     * @description: 课程下架
     * @param: courseId
     * @return: cn.clq.ymcc.result.JSONResult
     * @author clqlongbao
     * @date: 2022/7/25 16:46
     */
    @PostMapping("/offLineCourse/{courseId}")
    public JSONResult offLineCourse(@PathVariable("courseId") Long courseId){
        courseService.offLineCourse(courseId);
        return JSONResult.success();
    }

    /**
    * 保存和修改公用的
    */
    @PostMapping("/save")
    public JSONResult save(@RequestBody @Valid CourseDto dto){
        courseService.save(dto);
        return JSONResult.success();
    }

    /**
    * 删除对象
    */
    @DeleteMapping("/{id}")
    public JSONResult delete(@PathVariable("id") Long id){
        courseService.deleteById(id);
        return JSONResult.success();
    }

    /**
   * 获取对象
   */
    @GetMapping(value = "/{id}")
    public JSONResult get(@PathVariable("id")Long id){
        return JSONResult.success(courseService.selectById(id));
    }


    /**
    * 查询所有对象
    */
    @GetMapping("/list")
    public JSONResult list(){
        return JSONResult.success(courseService.selectList(null));
    }


    /**
     * 带条件分页查询数据
     */
    @PostMapping("/pagelist")
    public JSONResult page(@RequestBody CourseQuery query){
        Page<Course> page = new Page<Course>(query.getPage(),query.getRows());
        Wrapper<Course> tWrapper = new EntityWrapper<Course>();
        tWrapper.like("name",query.getKeyword());
        page = courseService.selectPage(page,tWrapper);//有高级查询条件
        //page = courseService.selectPage(page); 没有高级查询条件
        return JSONResult.success(new PageList<Course>(page.getTotal(),page.getRecords()));
    }
}
